//
//  MMViewController.m
//  MaterialDesignSpinner
//
//  Created by Mike Maxwell on 12/28/2014.
//  Copyright (c) 2014 Mike Maxwell. All rights reserved.
//

#import "MMViewController.h"

#import "MMMaterialDesignSpinner.h"

@interface MMViewController ()
@property (nonatomic, strong) IBOutlet MMMaterialDesignSpinner *spinnerView;
@property (weak, nonatomic) IBOutlet UISlider *sliderControl;
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;

@property (nonatomic, assign) BOOL animate;
@end

@implementation MMViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.spinnerView startAnimating];
}

#pragma mark Actions

- (IBAction)changeColorButtonTouched:(id)sender {
    self.spinnerView.tintColor = [MMViewController randomColor];
}

- (IBAction)changeLineWidthButtonTouched:(id)sender {
    self.spinnerView.lineWidth = (rand() % 20) / 2.f;
    if (self.spinnerView.lineWidth == 0) self.spinnerView.lineWidth = 0.1f;
}

- (IBAction)sliderValueUpdated:(id)sender {
    if (self.animate) {
        [self.spinnerView stopAnimating];
        self.spinnerView.duration = self.sliderControl.value;
        [self.spinnerView startAnimating];
    }
}

- (IBAction)sliderValueChanged:(id)sender {
    if (!self.animate) {
        self.spinnerView.percentComplete = [self sliderPercentValue];
    }
}

- (IBAction)squareCapButtonPressed:(id)sender {
    self.spinnerView.lineCap = kCALineCapSquare;
}

- (IBAction)rouncCapButtonPressed:(id)sender {
    self.spinnerView.lineCap = kCALineCapRound;
}

- (IBAction)buttCapButtonPressed:(id)sender {
    self.spinnerView.lineCap = kCALineCapButt;
}

- (IBAction)animateSwitchValueDidChange:(UISwitch *)sender {
    self.animate = sender.on;
    [self.spinnerView setAnimating:self.animate];
    self.sliderLabel.text = (self.animate
                             ? @"Duration"
                             : @"Percent Complete");
    self.spinnerView.percentComplete = [self sliderPercentValue];
}

#pragma mark Helper methods

+ (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (float)sliderPercentValue {
    return self.sliderControl.value / self.sliderControl.maximumValue;
}

@end
